package cst.a220tbc.weatherbyself;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cst.a220tbc.weatherbyself.bean.WeatherInfo;
import cst.a220tbc.weatherbyself.listener.WeatherListener;
import cst.a220tbc.weatherbyself.model.Weather51Model;
import cst.a220tbc.weatherbyself.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, WeatherListener {
    private EditText cityid;
    private TextView city;
    private TextView temp;
    private TextView city_ws;
    private TextView city_wd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        cityid = (EditText) findViewById(R.id.cityid);
        city = (TextView) findViewById(R.id.city);
        temp = (TextView) findViewById(R.id.temp);
        city_ws = (TextView) findViewById(R.id.ws);
        city_wd = (TextView) findViewById(R.id.wd);

        //获取按钮控件并设置按钮的点击事件监听器
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        Log.v("===========hehaha","1111111111");
        System.out.print("============222222222222222222222====================================");
        System.out.print("============333333333333333333333333333333333333=====================");
        System.out.println();
        Log.v("===========hehaha","114444444444444444444441");
    }
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                //普通创建对象方式
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(cityid.getText().toString(), this);
                break;
            case R.id.button2:

                //普通创建对象方式
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(cityid.getText().toString(), this);
                //工厂模式
                break;
            default:
                break;
        }
    }
    //获取数据成功
    @Override
    public void onResponse(WeatherInfo weather) {
        //显示获得的天气数据
        if (weather != null) {
            city.setText(weather.getCity());
            temp.setText(weather.getTemp());
            city_ws.setText(weather.getWS());
            city_wd.setText(weather.getWD());
        } else {
            city.setText("未知");
        }

    }

    //获取数据失败
    @Override
    public void onFail(String msg) {
        city.setText(msg);
    }

}
